<template>
  <div>
    <div class="search">
      <el-select v-model="Category" class="m-2" placeholder="全部">
        <el-option label="全部" value="" />
        <el-option
          v-for="item in options"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <div class="aaa">
        <el-input
          v-model="search"
          placeholder="搜索"
          @keyup.enter="getGodsList"
        />
      </div>
      <el-button color="#626aef" @click="getGodsList">搜索</el-button>
    </div>
    <el-table :data="resetGoodsList" style="width: 100%">
      <el-table-column type="index" label="序号" align="center" width="100px" />

      <el-table-column label="图片" align="center">
        <template #default="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.img1"
            :zoom-rate="1"
            :preview-src-list="[
              scope.row.img1,
              scope.row.img2,
              scope.row.img3,
              scope.row.img4,
            ]"
            :initial-index="4"
            fit="cover"
            preview-teleported
          />
        </template>
      </el-table-column>

      <el-table-column label="名称" align="center">
        <template #default="scope">
          <div class="text">
            {{ scope.row.proname }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="brand" label="品牌" align="center" />
      <!-- -------------------------筛选------------------------------- -->
      <el-table-column
        align="center"
        prop="category"
        label="分类"
        column-key="date"
        :filters="options.map((item) => ({ text: item, value: item }))"
        :filter-method="filterHandler"
      />
      <!-- -------------------------排序------------------------------- -->
      <el-table-column
        prop="originprice"
        sortable
        label="原价"
        align="center"
      />
      <el-table-column prop="discount" sortable label="折扣" align="center" />
      <el-table-column prop="sales" sortable label="销量" align="center" />
      <!-- -----------------！！！！！！！！！！！---------------------- -->

      <!-- -------------------------操作区------------------------------- -->

      <el-table-column label="筛选" align="center">
        <template #default="scope">
          <el-switch
            v-model="scope.row.isseckill"
            style="
              --el-switch-on-color: #13ce66;
              --el-switch-off-color: #ff4949;
            "
            :active-value="1"
            :inactive-value="0"
            @change="
              handleChange(scope.row.isseckill, scope.row.proid, 'isseckill')
            "
          />
        </template>
      </el-table-column>

      <!-- -------------------------删除------------------------------- -->
      <el-table-column label="是否售卖" align="center">
        <template #default="scope">
          <el-switch
            v-model="scope.row.issale"
            :active-value="1"
            :inactive-value="0"
            @change="handleChange(scope.row.issale, scope.row.proid, 'issale')"
            style="
              --el-switch-on-color: #13ce66;
              --el-switch-off-color: #ff4949;
            "
          />
        </template>
      </el-table-column>

      <el-table-column label="是否推荐" align="center">
        <template #default="scope">
          <el-switch
            v-model="scope.row.isrecommend"
            :active-value="1"
            :inactive-value="0"
            @change="
              handleChange(
                scope.row.isrecommend,
                scope.row.proid,
                'isrecommend'
              )
            "
            style="
              --el-switch-on-color: #13ce66;
              --el-switch-off-color: #ff4949;
            "
          />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button size="small" type="danger" @click="handleDelete"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="goodsList.length"
      v-model:current-page="page"
      :hide-on-single-page="true"
      :page-size="limitNum"
    />
  </div>
</template>

<script>
import { filtr, filtrateList, updateFlag } from "../../service/product.js";
export default {
  data() {
    return {
      options: [],
      list: [],
      Category: "",
      search: "",
      goodsList: [],
      page: 1,
      limitNum: 10,
    };
  },
  methods: {
    handleChange(flag, proid, type) {
      updateFlag(flag, proid, type).then((res) => {
        this.getGodsList();
      });
    },
    // 筛选
    filterHandler(value, row, column) {
      const property = column["property"];
      return row[property] === value;
    },
    // 获取数据
    getGodsList() {
      filtrateList({ category: this.Category, search: this.search }).then(
        (res) => {
          this.goodsList = res.data;
          (this.Category = ""), (this.search = "");
        }
      );
    },
    //删除
    handleDelete() {
      console.log(111);
    },
  },
  computed: {
    resetGoodsList() {
      return this.goodsList.slice((this.page - 1) * 10, this.page * 10);
    },
  },
  created() {
    // 获取商品列表数据
    this.getGodsList();
    filtr().then((res) => {
      this.options = res.data;
    });
  },
};
</script>

<style scoped>
.el-pagination {
  display: flex;
  justify-content: center;
}
img {
  width: 100%;
}
:deep(.cell) {
  height: 90px;
  line-height: 90px;
}
.text {
  height: 80px;
  line-height: 16px;
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 5;
}
.search {
  height: 40px;
  margin: 10px 0;
  display: flex;
  align-items: center;
}
.aaa {
  margin: 0 10px;
  width: 300px;
}
</style>
